//
//  HGGoodsHeaderView.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/8/3.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGGoodsHeaderView.h"
#import "HGGoodsModel.h"

@implementation HGSubtractView

- (instancetype)init {
    if (self = [super init]) {
        
        _index += 6;
        
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        self.layer.borderColor = kColorBackground.CGColor;
        self.layer.borderWidth = 1;
        
        UIButton *leftButton = [[UIButton alloc] init];
        leftButton.tag = 5;
        [leftButton setImage:[UIImage imageNamed:@"jian"] forState:(UIControlStateNormal)];
        [leftButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [leftButton addTarget:self action:@selector(cumulative:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:leftButton];
        
        [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(35, 30));
        
        }];
        
        UIButton *rigthButton = [[UIButton alloc] init];
        rigthButton.tag = 6;
        [rigthButton setImage:[UIImage imageNamed:@"jia"] forState:(UIControlStateNormal)];
        [rigthButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [rigthButton addTarget:self action:@selector(cumulative:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:rigthButton];
        
        [rigthButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.right.equalTo(self);
            make.size.equalTo(leftButton);
        }];
        
        
        _numLabel = [[UILabel alloc] init];
        _numLabel.text = @(_index).description;
        _numLabel.textAlignment = NSTextAlignmentCenter;
        _numLabel.textColor = kColorMainTheme;
        [self addSubview:_numLabel];
        
        [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(rigthButton.mas_left);
            make.left.equalTo(leftButton.mas_right);
            make.top.bottom.equalTo(self);
        }];
        
    }
    return self;
}

- (void)setIndex:(NSInteger)index {
    _index = index;
    _numLabel.text = @(_index).description;
}

- (void)cumulative:(UIButton *)sender {
    if (sender.tag == 5 && _index == 6) {
        [[XNProgressHUD shared] showInfoWithTitle:@"最低6瓶哦！"];
        return;
    }
    _index = sender.tag == 5 ? _index-6 : _index+6;
    _numLabel.text = @(_index).description;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"cumulative" object:nil userInfo:@{@"count" : _numLabel.text}];
}

@end

@interface HGGoodsHeaderView ()

//标题
@property (strong, nonatomic) UILabel *titleLabel;
//已售
@property (strong, nonatomic) UILabel *soldLabel;
//剩余
@property (strong, nonatomic) UILabel *numberLabel;
//金额
@property (strong, nonatomic) UILabel *moenyLabel;
//类型
@property (strong, nonatomic) UILabel *typeLabel;

@property (copy, nonatomic) NSMutableArray<UIButton *> *typeButtons;

//加减控件
@property (strong, nonatomic) HGSubtractView *subtractView;

@end

@implementation HGGoodsHeaderView

- (instancetype)init {
    if (self = [super init]) {
        
        [self initSubViews];
        
    }
    return self;
}

- (void)setModel:(HGGoodsModel *)model {
    _model = model;
    _titleLabel.text = model.name;
    _moenyLabel.text = model.rmbPrice;
    _numberLabel.text = [NSString stringWithFormat:@"剩余：%@",model.stock];
    _soldLabel.text = [NSString stringWithFormat:@"已售：%ld",model.sold];
}

- (void)initSubViews {
    
    CGFloat margin = 14;
    
    CGFloat H = 48;
    
    UIView *infoView = [[UIView alloc] init];
    [self addSubview:infoView];
    
    [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.offset(80);
    }];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.text = @"xxxxx";
    _titleLabel.numberOfLines = 2;
    _titleLabel.font = [UIFont systemFontOfSize:16];
    _titleLabel.textColor = kColorDefult;
    [infoView addSubview:_titleLabel];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(margin);
    }];
    
    _soldLabel = [[UILabel alloc] init];
    _soldLabel.text = @"已售:0";
    _soldLabel.font = [UIFont systemFontOfSize:12];
    _soldLabel.textColor = kColorText;
    [infoView addSubview:_soldLabel];
    
    [_soldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom).offset(margin + 6);
        make.left.equalTo(_titleLabel);
    }];
    
    _numberLabel = [[UILabel alloc] init];
    _numberLabel.text = @"剩余:0";
    _numberLabel.font = [UIFont systemFontOfSize:12];
    _numberLabel.textColor = kColorText;
    [infoView addSubview:_numberLabel];
    
    [_numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_soldLabel);
        make.left.equalTo(_soldLabel.mas_right).offset(45);
    }];
    
    
    _moenyLabel = [[UILabel alloc] init];
    _moenyLabel.text = @"￥000.00";
    _moenyLabel.font = [UIFont systemFontOfSize:20];
    _moenyLabel.textColor = kColorMainTheme;
    [infoView addSubview:_moenyLabel];
    
    [_moenyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_numberLabel);
        make.right.offset(-margin);
    }];
    
    
    UIView *typeView = [[UIView alloc] init];
    [self addSubview:typeView];
    
    [typeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(infoView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.offset(H);
    }];
    
    
    _typeLabel = [[UILabel alloc] init];
    _typeLabel.text = @"类型：6支/箱";
    _typeLabel.font = [UIFont systemFontOfSize:14];
    _typeLabel.textColor = kColorText;
    [infoView addSubview:_typeLabel];
    
    [_typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(typeView);
        make.left.offset(margin);
    }];
    
    UIView *numView = [[UIView alloc] init];
    [self addSubview:numView];
    
    [numView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(typeView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.offset(H);
    }];
    
    UILabel *slLabel = [[UILabel alloc] init];
    slLabel.text = @"数量";
    slLabel.font = [UIFont systemFontOfSize:14];
    slLabel.textColor = kColorText;
    [numView addSubview:slLabel];
    
    [slLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(numView);
        make.left.offset(margin);
    }];
    
    _subtractView = [[HGSubtractView alloc] init];
    [numView addSubview:_subtractView];
    [_subtractView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(slLabel);
        make.right.offset(-margin);
        make.width.offset(115);
    }];
    
    UIView *distributionView = [[UIView alloc] init];
    [self addSubview:distributionView];
    
    [distributionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(numView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.offset(H);
    }];
    
    UILabel *psLabel = [[UILabel alloc] init];
    psLabel.text = @"配送方式";
    psLabel.font = [UIFont systemFontOfSize:14];
    psLabel.textColor = kColorText;
    [distributionView addSubview:psLabel];
    
    [psLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(distributionView);
        make.left.offset(margin);
    }];
    
    UIView *seletedView = [[UIView alloc] init];
    [distributionView addSubview:seletedView];
    
    [seletedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-margin);
        make.top.bottom.equalTo(distributionView);
        make.width.offset(200);
    }];
    
    NSArray *titles = @[@"自提",@"平邮",@"快递"];
    NSArray *typeTag = @[@"2",@"3",@"1"];
    for (int i = 0; i < titles.count ; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = [typeTag[i] integerValue];
        [button setTitle:titles[i] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [button setBackgroundImage:[UIImage imageNamed:@"sc_xz"] forState:(UIControlStateSelected)];
        [button setBackgroundImage:[UIImage imageNamed:@"sc_no"] forState:(UIControlStateNormal)];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(changeType:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.typeButtons addObject:button];
        [seletedView addSubview:button];
    }
    
    [seletedView.subviews mas_distributeViewsAlongAxis:(MASAxisTypeHorizontal) withFixedSpacing:23 leadSpacing:0 tailSpacing:0];
    [seletedView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(seletedView);
    }];
    
    
    [self.subviews enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = kColorBackground;
        [obj addSubview:lineView];
        
        if (idx == 0) {
            UIView *lineView = [[UIView alloc] init];
            lineView.backgroundColor = kColorBackground;
            [obj addSubview:lineView];
            
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.equalTo(obj);
                make.height.offset(1);
            }];
        }
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(obj);
            make.height.offset(1);
            make.left.offset(margin);
        }];
    }];
    
    UIView *lineView = self.subviews.lastObject;
    
    UILabel *laebl = [[UILabel alloc] init];
    laebl.text = @"商品详情";
    laebl.font = [UIFont systemFontOfSize:14];
    laebl.textColor = kColorText;
    [distributionView addSubview:laebl];
    
    [laebl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom).offset(margin);
        make.left.offset(margin);
    }];
}

- (void)changeType:(UIButton *)button {
    __block NSInteger i = 0;
    [_typeButtons enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj == button) {
            obj.selected = YES;
            i = obj.tag;
            return;
        }
        obj.selected = NO;
    }];
    
    if (_delegate && [_delegate respondsToSelector:@selector(chooseType:)]) {
        [_delegate chooseType:i];
    }
}

- (NSMutableArray<UIButton *> *)typeButtons {
    if (!_typeButtons) {
        _typeButtons = [@[] mutableCopy];
    }
    return _typeButtons;
}

@end
